/**
 * Base namespace for goddy library.
 */
var goddy = goddy || {};

/**
 * Reference to global context.
 */
goddy.global = this;

/**
 * Return an object based on the name.
 * 
 * @param {string} name: The fully qulified name.
 * @param [object] base_object: The base object to look in.
 * 
 * @return {object} The object or null if not found.
 */
goddy.to_object = function(name,base_object){
	var parts = name.split(".");
	var cur_obj = base_object || goddy.global;
	for(var part; parts.length && (part = parts.shift()); ){
		if(!cur_obj[part]){
			return null;
		}
		cur_obj = cur_obj[part];
	}
	return cur_obj;
}


goddy.set_object = function(/*string*/name, /*object*/value){
	var parts = name.split("."), 
	last_part = parts.pop(),
	obj = goddy.export_by_attr(parts);
	return obj && last_part ? obj[last_part] = value : undefined;
}


//Todo: documentation
goddy.provide = function(name){
	if(goddy.to_object(name)){
		alert("sorry the object has existed!");
	}
	goddy.export_path(name);
}


//Todo: documentation\
goddy.require = function(){
	
}


//Todo: documentation
goddy.export_path = function(name){
	if(!name){ return; }
	goddy.export_by_attr(name.split("."));
	
}



goddy.export_by_attr = function(/*array*/parts){
	var cur = goddy.global;
	for(var part; parts.length && (part = parts.shift());){
		if(cur[part]){
			cur = cur[part];
		}else{
			cur = cur[part] = {};
		}
	}
	return cur;
}


//todo: document
goddy.is_string = function(val){
	return typeof val === "string";
}


//alias of goddy
var $g = goddy;

